<?php

namespace App\Http\Controllers\Admin;

use App\Models\Notice;
use App\Models\FangOwner;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class NoticeController extends BaseController
{
    /**
     * 列表
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        // 获取搜索框 
        // 开启时间
        $datemin = $request->get('datemin');
        // 结束时间
        $datemax = $request->get('datemax');
        // 搜索关键字
        $kw = $request->get('kw', '');
        // 查询对象
        $query = Notice::with(['owner']);
        // 日期
        if (!empty($datemin) && !empty($datemax)){
            if(strlen($datemin) < 18){
                // 开始时间
                $datemin = $datemin.' 00:00:00';
            }
            if(strlen($datemax) < 18){
                // 结束时间
                $datemax = $datemax.' 23:59:59';
            }
            $query->whereBetween('created_at',[$datemin,$datemax]);
        }
        // 搜索关键词
        if(!empty($kw)){
            $query->where('status', 'like', "%{$kw}%");
            $query->orWhere('cnt', 'like', "%{$kw}%");
            $owner = FangOwner::where('name', 'like', "%{$kw}%")->get('id')->toArray();
            $owner = count($owner) ? $owner[0]['id'] : null;
            if($owner){
                $query->orWhere('fangowner_id', 'like', "%{$owner}%");
            }
        }
        // 关联取出数据
        $data = $query->paginate($this->pagesize);
        return view('admin.notice.index',compact('data'));
    }

    /**
     * 添加显示
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $owner = (new FangOwner())->get(['id','name']);
        return view('admin.notice.create',compact('owner'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // 表单验证
        $this->validate($request,[
            'fangowner_id' => 'required',
            'renting_id' => 'required',
            'dtime' => 'required',
            'cnt' => 'required',
        ]);
        // 入库操作
        $postDate = $request->except(['_token']);
        Notice::create($postDate);
        // 此处不需要写任何添加完成后的代码，我们在事件观察中写
        // 跳转
        return redirect(route('admin.notice.index'));
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Notice  $notice
     * @return \Illuminate\Http\Response
     */
    public function show(Notice $notice)
    {
        //
    }

    /**
     * 修改预约
     *
     * @param  \App\Models\Notice  $notice
     * @return \Illuminate\Http\Response
     */
    public function edit(Notice $notice)
    {
        $owner = (new FangOwner())->get(['id','name']);
        return view('admin.notice.edit',compact('notice','owner'));
    }

    /**
     * 修改预约入库操作
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Notice  $notice
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Notice $notice)
    {
        // 表单验证
        $this->validate($request,[
            'fangowner_id' => 'required',
            'renting_id' => 'required',
            'dtime' => 'required',
            'cnt' => 'required',
        ]);
        // 入库操作
        $postDate = $request->except(['_method','_token']);
        $notice -> update($postDate);
        return redirect(route('admin.notice.index'));
    }

    /**
     * 删除预约
     *
     * @param  \App\Models\Notice  $notice
     * @return \Illuminate\Http\Response
     */
    public function destroy(Notice $notice)
    {
        $notice->delete();
        return ['status' => 0,'msg' => '删除成功'];
    }
    // 批量删除
    public function delall(Request $request)
    {
        $ids = $request->get('id');
        Fang::destroy($ids);
        return ['status' => 0, 'msg' => '批量删除成功'];
    }
}
